/* 
 * File:   Synch.h
 * Author: ben
 *
 * Created on November 11, 2012, 12:31 PM
 */

#ifndef SYNCH_H
#define	SYNCH_H

#include <vector>
#include "Locker.h"
#include "Thread.h"

class Synch : public Locker {
private:
    bool m_pauseThreads;
    bool m_sleepThreads;
    bool m_terminateThreads;
    static std::vector<Thread *> m_threads;
public:
    Synch();
    // Called by agents

    /**
     * Accessor to m_pauseThreads
     * @return True if agents should pause
     */
    bool pause() {
        return m_pauseThreads;
    }
    /**
     * Acessor to m_sleepThreads
     * @return True if agents should sleep
     */
    bool sleep() {
        return m_sleepThreads;
    }
    /**
     * Starts an agent by adding it to the list of all running agents
     * @param thread Thread to start
     */
    void startAgent(Thread *thread);

    /**
     * Signals threads to terminate at next appropriate location
     */
    void terminateThreads();
    
    /**
     * Acessor to m_terminateThreads
     * @return True if agents should terminate
     */
    bool terminate() { return m_terminateThreads; }
    // Called by main

    /**
     * Cases all threads to pause at next appropriate location
     */
    void pauseThreads() {
        m_pauseThreads = true;
    }
    /**
     * Resumes all threads
     */
    void resumeThreads();

    /**
     * Causes all threads to sleep periodically while executing
     */
    void sleepThreads() {
        m_sleepThreads = true;
    }

    /**
     * Threads will not sleep periodically while executing
     */
    void unsleepThreads() {
        m_sleepThreads = false;
    }

};

#endif	/* SYNCH_H */

